<?php

/**

 * 我的商城

 *

 *

 *

 * * @网店运维 (c) 2015-2018  Inc. (http://www.shopwwi.com)
 * @license    http://www.shopwwi.c om
 * @link       交流群号：111731672
 * @since      网店运维提供技术支持 授权请购买shopnc授权

 */



//use Shopnc\Tpl;

header("Content-Type: text/html;charset=utf-8");

defined('InShopNC') or exit('Access Invalid!');



class member_indexControl extends mobileHomeControl {



    public function __construct(){

        parent::__construct();
        $this->isLogin();
    }



    /**

     * 我的商城

     */

    public function indexOp() {

        $member_info = array();
		if($_SESSION['member_id']) {
            $model_member = Model('member');
            $member_info = $model_member->getMemberInfoByID($_SESSION['member_id']);
            if ($member_info){
                $member_gradeinfo = $model_member->getOneMemberGrade(intval($member_info['member_exppoints']));
                $member_info = array_merge($member_info,$member_gradeinfo);
                $member_info['security_level'] = $model_member->getMemberSecurityLevel($member_info);
            }
        }

        $member_info['user_name'] = $this->member_info['member_name'];
        $member_info['avatar'] = getMemberAvatarForID($this->member_info['member_id']);
        $member_info['point'] = $this->member_info['member_points'];
        $member_info['predepoit'] = $this->member_info['available_predeposit'];
        $member_info['available_rc_balance'] = $this->member_info['available_rc_balance'];
		$model_order = Model('order');
		$member_info['order_nopay_count'] = $model_order->getOrderCountByID('buyer',$_SESSION['member_id'],'NewCount');
        $member_info['order_noreceipt_count'] = $model_order->getOrderCountByID('buyer',$_SESSION['member_id'],'SendCount');
        $member_info['order_noeval_count'] = $model_order->getOrderCountByID('buyer',$_SESSION['member_id'],'EvalCount');

		$member_info['favorites_goods'] = Model('favorites')->getGoodsFavoritesCountByGoodsId(array(),$_SESSION['member_id']);
		$member_info['favorites_store'] = Model('favorites')->getStoreFavoritesCountByStoreId(array(),$_SESSION['member_id']);



        output_data(array('member_info' => $member_info));


    }

	public function my_assetOp() {


        $member_info['point'] = $this->member_info['member_points'];
        $member_info['predepoit'] = $this->member_info['available_predeposit'];
		$member_info['available_rc_balance'] = $this->member_info['available_rc_balance'];
		$member_info['voucher'] = Model('voucher')->getCurrentAvailableVoucherCount($_SESSION['member_id']);
		$member_info['redpacket'] = Model('redpacket')->getCurrentAvailableRedpacketCount($_SESSION['member_id']);
        output_data($member_info);
    }

    public function member_indexOp() {
        $member_info = array();
        if($_SESSION['member_id']) {
            $model_member = Model('member');
            $member_info = $model_member->getMemberInfoByID($_SESSION['member_id']);
            if ($member_info){
                $member_gradeinfo = $model_member->getOneMemberGrade(intval($member_info['member_exppoints']));
                $member_info = array_merge($member_info,$member_gradeinfo);
                $member_info['security_level'] = $model_member->getMemberSecurityLevel($member_info);
            }
        }

        $member_info['user_name'] = $this->member_info['member_name'];
        $member_info['avatar'] = getMemberAvatarForID($this->member_info['member_id']);
        $member_info['point'] = $this->member_info['member_points'];
        $member_info['predepoit'] = $this->member_info['available_predeposit'];
        $member_info['available_rc_balance'] = $this->member_info['available_rc_balance'];
        $model_order = Model('order');
        $member_info['order_nopay_count'] = $model_order->getOrderCountByID('buyer',$_SESSION['member_id'],'NewCount');
        $member_info['order_noreceipt_count'] = $model_order->getOrderCountByID('buyer',$_SESSION['member_id'],'SendCount');
        $member_info['order_noeval_count'] = $model_order->getOrderCountByID('buyer',$_SESSION['member_id'],'EvalCount');

        $member_info['favorites_goods'] = Model('favorites')->getGoodsFavoritesCountByGoodsId(array(),$_SESSION['member_id']);
        $member_info['favorites_store'] = Model('favorites')->getStoreFavoritesCountByStoreId(array(),$_SESSION['member_id']);
        $member_info['voucher_count'] = Model('voucher')->getCurrentAvailableVoucherCount($_SESSION['member_id']);
        $member_info['redpacket_count'] = Model('redpacket')->getCurrentAvailableRedpacketCount($_SESSION['member_id']);
        Tpl::output('home_member_info',$member_info);
        Tpl::showpage('member/member_index');
    }
    public function member_addressOp() {
        $model_address = Model('address');
        $address_list = $model_address->getAddressList(array('member_id'=>$_SESSION['member_id']));
        Tpl::output('address_list',$address_list);
        Tpl::showpage('member/member_address');
    }
    public function member_address2Op() {
        $data=array( 'address_id'=>$_POST['id'] );
        $model_address = Model('address');
        $msg = $model_address->dellAddress($data);

        echo json_encode(array("feedback"=>"get it!".$_POST["action"]."|".$_POST["id"].$msg));
    }
    public function member_address3Op() {
        $data=array();
        $data['is_default']=1 ;
        $set_zero=array( 'is_default'=>0 );
        $default=$_POST['default'];
        $model_address = Model('address');
        $model_address->clearAddress($_SESSION['member_id'], $data['is_default'], $set_zero);
        $count=$model_address->set_default($default, $data);  echo $count;

    }
    public function member_address_addOp() {
        $member_info = $this->member_info;
        Tpl::output('home_member_info',$member_info);
        Tpl::showpage('member/member_address_add');
    }

    public function member_address_mobileOp() {
        $member_info = $this->member_info;
        Tpl::output('home_member_info',$member_info);
        $address_prov=$_POST['prov'];
        $address_city=$_POST['city'];
        $address_region=$_POST['region'];
        $address_all= $address_prov.' '.$address_city.' '.$address_region;
        $address=Model('address');
        $address_c_id= $address->address_id($address_city);
        $address_r_id= $address->address_id($address_region);
        $address_city_id=$address_c_id[0]['area_id'];
        $address_region_id=$address_r_id[0]['area_id'];

        $data = array();
           $data['member_id'] = $_SESSION['member_id'];
           $data['true_name'] = $_POST['true_name'];
           $data['area_id'] =  intval($address_region_id);
           $data['city_id'] =  intval($address_city_id);
           $data['area_info'] = $address_all;
           $data['address'] = $_POST['vaddress'];
           $data['tel_phone'] = $_POST['tel_phone'];
           $data['mob_phone'] = $_POST['mob_phone'];
           $data['is_default'] = $_POST['address'] ? 1 : 0;

           //将原来的默认地址取消
           if($data['is_default']==1){
               $set_zero=array( 'is_default'=>0 );
               $address->clearAddress($_SESSION['member_id'], $data['is_default'], $set_zero);
           }

           //检测地址是否超过20条
            $check_address=array('member_id'=>$_SESSION['member_id']);
            $count_address=$address->countAddress($check_address);
            if($count_address >20){
                echo "<script>alert('地址已超过最大限制');window.location.href=\"/mobile/index.php?act=member_index&op=member_address\";
            </script>";
            }

           $insert = $address->writeAddress($data);

            if($insert){
                echo "<script>alert('设置成功');window.location.href=\"/mobile/index.php?act=member_index&op=member_address\";
            </script>";

        }else{
            echo "<script>alert('设置失败，请重试！')</script>";
            echo "<script>history.go(-1);</script>";
        }
        
    }

    public function member_settingOp() {
        $member_info = $this->member_info;
        $name_card=$_SESSION['member_id'];
        $model_modify = Model('member');
        $member_se=$model_modify->memberShow($name_card);

        Tpl::output('member_show',$member_se[0]['member_avatar']);
        Tpl::output('member_name',$member_se[0]['member_nickname']);
        Tpl::output('member_sex',$member_se[0]['member_sex']);
        Tpl::output('home_member_info',$member_info);
        Tpl::showpage('member/member_setting');
    }

    public function member_emailOp() {
        $member_info = $this->member_info;
        Tpl::output('home_member_info',$member_info);
        Tpl::showpage('member/member_email');
    }

    public function member_nameOp() {
        $member_info = $this->member_info;
        $name_card=$_SESSION['member_id'];
        $model_show = Model('member');
        $member_name = $model_show->memberShow($name_card);
        Tpl::output('member_name',$member_name[0]['member_nickname']);
        Tpl::output('home_member_info',$member_info);
       
        Tpl::showpage('member/member_name');
    }

   public function member_modifyOp(){
       $name_chance=$_POST['name_modify'];
       $name_card=$_SESSION['member_id'];
       $model_modify = Model('member');
       $member_se=$model_modify->memberNameModify($name_card,$name_chance);

       if($member_se){
           echo "<script>alert('修改成功');window.location.href=\"/mobile/index.php?act=member_index&op=member_setting\";  
         </script>";

       }else{
           echo "<script>alert('修改失败，请重试！')</script>";
           echo "<script>history.go(-1);</script>";
       }
   }

    public function member_sexOp() {
        $member_info = $this->member_info;

        $name_card=$_SESSION['member_id'];
        $model_show = Model('member');
        $member_name = $model_show->memberShow($name_card);
        
        Tpl::output('member_modify_sex',$member_name[0]['member_sex']);
        
        Tpl::output('home_member_info',$member_info);
        Tpl::showpage('member/member_sex');
    }

    public function member_passwordOp() {
        $member_info = $this->member_info;
        Tpl::output('home_member_info',$member_info);
        Tpl::showpage('member/member_password');
    }

    public function member_avatarOp() {
        $member_info = $this->member_info;
        Language::read('member_home_member,cut');
        $member_info = Model('member')->getMemberInfoByID($_SESSION['member_id'],'member_avatar');
        Tpl::output('member_avatar',$member_info['member_avatar']);
        self::profile_menu('member','avatar');
        Tpl::output('menu_sign','profile');
        Tpl::output('menu_sign_url','index.php?act=member_information&op=member');
        Tpl::output('menu_sign1','avatar');

        Tpl::output('home_member_info',$member_info);
        Tpl::showpage('member/member_avatar');
    }

    private function profile_menu($menu_type,$menu_key='') {
        $menu_array     = array();
        switch ($menu_type) {
            case 'member':
                $menu_array = array(
                    1=>array('menu_key'=>'member',  'menu_name'=>Language::get('home_member_base_infomation'),'menu_url'=>'index.php?act=member_information&op=member'),
                    2=>array('menu_key'=>'more',    'menu_name'=>Language::get('home_member_more'),'menu_url'=>'index.php?act=member_information&op=more'),
                    5=>array('menu_key'=>'avatar',  'menu_name'=>Language::get('home_member_modify_avatar'),'menu_url'=>'index.php?act=member_information&op=avatar'));
                break;
        }
        Tpl::output('member_menu',$menu_array);
        Tpl::output('menu_key',$menu_key);
    }

    public function uploadOp() {
//        if (!chksubmit()){
//            redirect('index.php?act=member_index&op=member_avatar');
//        }
        import('function.thumb');
        Language::read('member_home_member,cut');
        $lang	= Language::getLangContent();
        $member_id = $_SESSION['member_id'];

         if (empty($_FILES['pic']['name'])){
             echo "<script>alert('请选着图片！');window.location.href=\"/mobile/index.php?act=member_index&op=member_avatar\";</script>";
         }
        //上传图片
        $upload = new UploadFile();
        $upload->set('thumb_width', 132);
        $upload->set('thumb_height',132);
        $ext = strtolower(pathinfo($_FILES['pic']['name'], PATHINFO_EXTENSION));
        $upload->set('file_name',"avatar_.$ext");
        $upload->set('thumb_ext',$member_id);
        $upload->set('new_ext','jpg');
        $upload->set('ifremove',true);
        $upload->set('default_dir',ATTACH_AVATAR);
        $member_pho="avatar_$member_id.$ext";   
        $member_pic=Model('member')->memberPhoto($_SESSION['member_id'], $member_pho );

        if (!empty($_FILES['pic']['tmp_name'])){
            $result = $upload->upfile('pic');
            if ($result){   
                echo "<script>alert('上传成功！');window.location.href=\"/mobile/index.php?act=member_index&op=member_setting\";</script>";
//               showMessage($upload->error,'','html','error');

            }else{
//                echo "<script>alert('上传失败！')</script>";
                echo "<script>alert('上传失败,请换图！');window.location.href=\"/mobile/index.php?act=member_index&op=member_avatar\";</script>";
            }
        }else{
            showMessage('上传失败，请尝试更换图片格式或小图片','','html','error');
        }
        self::profile_menu('member','avatar');
        Tpl::output('menu_sign','profile');
        Tpl::output('menu_sign_url','index.php?act=member_information&op=member');
        Tpl::output('menu_sign1','avatar');
        Tpl::output('newfile',$upload->thumb_image);
        Tpl::output('height',get_height(BASE_UPLOAD_PATH.'/'.ATTACH_AVATAR.'/'.$upload->thumb_image));
        Tpl::output('width',get_width(BASE_UPLOAD_PATH.'/'.ATTACH_AVATAR.'/'.$upload->thumb_image));



    }

    public function fov_goodsOp() {
        $member_info = $this->member_info;


        $favorites_model = Model('favorites');


        $show = $_GET['show'];
        $men_key = 'fav_goods';
        $show_array = array(
            'pic'=>array(
                'favorites_goods_picshowlist',
                'fav_goods'
            ),
            'store'=>array(
                'favorites_goods_shoplist',
                'fav_goodsstore'
            )
        );
        $show = (array_key_exists($show,$show_array)) ? $show : 'pic';
        $show_type = $show_array[$show][0];
        $men_key = $show_array[$show][1];

        $condition = array();
        $condition['member_id'] = $_SESSION['member_id'];
        $keyword_type = array('store_name','log_msg','goods_name');
        if (trim($_GET['key']) != '' && in_array($_GET['type'],$keyword_type)){
            $type = $_GET['type'];
            $condition[$type] = array('like','%'.$_GET['key'].'%');
        }     
        $favorites_list = $favorites_model->getGoodsFavoritesList($condition, '*', 6);
        Tpl::output('show_page',$favorites_model->showpage(2));
        if (!empty($favorites_list) && is_array($favorites_list)){
            $favorites_id = array();//收藏的商品编号
            foreach ($favorites_list as $key=>$favorites){
                $fav_id = $favorites['fav_id'];
                $favorites_id[] = $favorites['fav_id'];
                $favorites_key[$fav_id] = $key;
            }
            $goods_model = Model('goods');
            $fields = 'goods.goods_id,goods.goods_name,goods.store_id,goods.goods_image,goods.goods_promotion_price,goods.goods_state,goods.goods_verify,goods.evaluation_count,goods.goods_salenum,goods.goods_collect,'
                .'goods.is_virtual,goods.is_fcode,goods.is_presell,goods.is_book,'
                .'store.store_name,store.member_id,store.member_name,store.store_qq,store.store_ww,store.store_domain,store.store_avatar';
            $goods_list = $goods_model->getGoodsStoreList(array('goods_id' => array('in', $favorites_id)), $fields);
            $store_array = array();//店铺编号
            if (!empty($goods_list) && is_array($goods_list)){
                $store_goods_list = array();//店铺为分组的商品
                foreach ($goods_list as $key=>$fav){
                    $fav['state'] = $goods_model->checkOnline($fav);
                    $fav_id = $fav['goods_id'];
                    $fav['goods_member_id'] = $fav['member_id'];
                    $key = $favorites_key[$fav_id];
                    $favorites_list[$key]['goods'] = $fav;
                    $store_id = $fav['store_id'];
                    if (!in_array($store_id,$store_array)) $store_array[] = $store_id;
                    $store_goods_list[$store_id][] = $favorites_list[$key];
                }
            }
            $store_favorites = array();//店铺收藏信息
            $voucher_template = array();
            if (!empty($store_array) && is_array($store_array)){
                $store_list = $favorites_model->getStoreFavoritesList(array('member_id'=>$_SESSION['member_id'], 'fav_id'=> array('in', $store_array)));
                if (!empty($store_list) && is_array($store_list)){
                    foreach ($store_list as $key=>$val){
                        $store_id = $val['fav_id'];
                        $store_favorites[] = $store_id;
                    }
                }
                $condition = array();
                $condition['voucher_t_gettype'] = 3;
                $condition['voucher_t_state'] = 1;
                $condition['voucher_t_end_date'] = array('gt', time());
                $condition['voucher_t_mgradelimit'] = array('elt', $this->member_info['level']);
                $condition['voucher_t_store_id'] = array('in', $store_array);
                $voucher_template = Model('voucher')->getVoucherTemplateList($condition);
                $voucher_template = array_under_reset($voucher_template, 'voucher_t_store_id', 2);
            }
        }
        $total_page = pagecmd('gettotalpage');
        if (intval($_GET['curpage'] > $total_page)) {
            exit();
        }


        Tpl::output('total_page', $total_page);
        self::profile_menu('favorites','favorites');
        Tpl::output('menu_key',$men_key);
        Tpl::output('favorites_list',$favorites_list);
        Tpl::output('store_favorites',$store_favorites);
        Tpl::output('voucher_template', $voucher_template);
        Tpl::output('store_goods_list',$store_goods_list);


        Tpl::output('home_member_info',$member_info);
        Tpl::showpage('member/fov_goods');
    }

    public function fov_storeOp() {
        $member_info = $this->member_info;

        $favorites_model = Model('favorites');
        //分页
        $page   = new Page();
        $page->setEachNum(2);
        $page->setStyle('admin');

        $favorites_list = $favorites_model->getStoreFavoritesList(array('member_id'=>$_SESSION['member_id']), '*',2);
        if (!empty($favorites_list) && is_array($favorites_list)){
            $favorites_id = array();//收藏的店铺编号
            foreach ($favorites_list as $key=>$favorites){
                $fav_id = $favorites['fav_id'];
                $favorites_id[] = $favorites['fav_id'];
                $favorites_key[$fav_id] = $key;
            }

            $store_model = Model('store');
            $store_list = $store_model->getStoreList(array('store_id'=>array('in', $favorites_id)));
            if (!empty($store_list) && is_array($store_list)){
                foreach ($store_list as $key=>$val){
                    $store_id = $val['store_id'];
                    $key = $favorites_key[$store_id];
                    $favorites_list[$key]['store'] = $val;
                    $favorites_list[$key]['goods'] = $this->_getStoreGoods($store_id);
                }
            }
        }
        self::profile_menu('favorites','fav_store');
        Tpl::output('menu_key',"fav_store");
        Tpl::output('favorites_list',$favorites_list);
        Tpl::output('show_page',$favorites_model->showpage(2));
//        Tpl::output('show_page',$page->show());



        Tpl::output('home_member_info',$member_info);
        Tpl::showpage('member/fov_store');
    }

    /**
     * 取得店铺上新、优惠促销、热销商品
     * @param unknown $store_id
     */
    private function _getStoreGoods($store_id) {
        $model_goods = Model('goods');
        $fieldstr = "goods_id,goods_name,goods_promotion_price,goods_image";

        // 查询数量
        $num = 5;

        $sign = '';

        // 热销商品
        $condition = array();
        $condition['store_id'] = $store_id;
        $hot_goods_list = $model_goods->getGoodsListByColorDistinct($condition, $fieldstr, 'goods_salenum desc', 0, $num);
        if (!empty($hot_goods_list)) {
            $sign = 'hot';
        }

        // 优惠促销
        $condition = array();
        $condition['store_id'] = $store_id;
        $condition['goods_promotion_type'] = array('neq', 0);
        $promotion_goods_list = $model_goods->getGoodsListByColorDistinct($condition, $fieldstr, 'goods_id desc', 0, $num);
        if (!empty($promotion_goods_list)) {
            $sign = 'promotion';
        }

        // 本周上新
        $weak_start = mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y"));
        $weak_end   = mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"));
        $condition = array();
        $condition['store_id'] = $store_id;
        $condition['goods_addtime'] = array('between', array($weak_start, $weak_end));
        $new_goods_list = $model_goods->getGoodsListByColorDistinct($condition, $fieldstr, 'goods_id desc', 0, $num);
        if (!empty($new_goods_list)) {
            $sign = 'new';
        }

        return array('new'=>$new_goods_list, 'promotion'=>$promotion_goods_list, 'hot'=>$hot_goods_list, 'sign' => $sign);
    }


    public function member_recordOp() {
        $member_info = $this->member_info;

        $model = Model('goods_browse');
        //商品分类缓存
        $gc_list = Model('goods_class')->getGoodsClassForCacheModel();
        //查询浏览记录
        $where = array();
        $where['member_id'] = $_SESSION['member_id'];
        $gc_id = intval($_GET['gc_id']);
        if ($gc_id > 0){
            $where['gc_id_'.$gc_list[$gc_id]['depth']] = $gc_id;
        }
        $browselist_tmp = $model->getGoodsbrowseList($where, '', 6, 0, 'browsetime desc');
        $browselist = array();
        foreach ((array)$browselist_tmp as $k=>$v){
            $browselist[$v['goods_id']] = $v;
        }
        //查询商品信息
        $browselist_new = array();
        if ($browselist){
            $goods_list_tmp = Model('goods')->getGoodsList(array('goods_id' => array('in', array_keys($browselist))), 'goods_id, goods_name, goods_promotion_price,goods_promotion_type, goods_marketprice, goods_image, store_id, gc_id, gc_id_1, gc_id_2, gc_id_3');
            $goods_list = array();
            foreach ((array)$goods_list_tmp as $v){
                $goods_list[$v['goods_id']] = $v;
            }
            foreach ($browselist as $k=>$v){
                if ($goods_list[$k]){
                    $tmp = array();
                    $tmp = $goods_list[$k];
                    $tmp["browsetime"] = $v['browsetime'];
                    if (date('Y-m-d',$v['browsetime']) == date('Y-m-d',time())){
                        $tmp['browsetime_day'] = '今天';
                    } elseif (date('Y-m-d',$v['browsetime']) == date('Y-m-d',(time()-86400))){
                        $tmp['browsetime_day'] = '昨天';
                    } else {
                        $tmp['browsetime_day'] = date('Y年m月d日',$v['browsetime']);
                    }
                    $tmp['browsetime_text'] = $tmp['browsetime_day'].date('H:i',$v['browsetime']);
                    $browselist_new[] = $tmp;
                }
            }
        }

        //查询浏览记录商品分类
        $browseclass_list = $model->getGoodsbrowseList(array('member_id'=>$_SESSION['member_id']), 'gc_id, min(gc_id_1) as gc_id_1, min(gc_id_2) as gc_id_2, min(gc_id_3) as gc_id_3', 0, 0, 'gc_id desc', 'gc_id');
        $browseclass_arr = array();
        foreach((array)$browseclass_list as $k=>$v){
            if($v['gc_id_1'] > 0 && !$browseclass_arr[$v['gc_id_1']]){
                $browseclass_arr[$v['gc_id_1']] = array('gc_name'=>$gc_list[$v['gc_id_1']]['gc_name'],'sonclass'=>array());
            }
            if($v['gc_id_2'] > 0){
                $browseclass_arr[$v['gc_id_1']]['sonclass'][$v['gc_id_2']] = array('gc_name'=>$gc_list[$v['gc_id_2']]['gc_name'],'sonclass'=>array());
            }
        }
        Tpl::output('browseclass_arr',$browseclass_arr);
        Tpl::output('browselist',$browselist_new);
        Tpl::output('show_page',$model->showpage());


        Tpl::output('home_member_info',$member_info);
        Tpl::showpage('member/member_record');
    }

    public function member_couponOp() {
        $member_info = $this->member_info;
        Tpl::output('home_member_info',$member_info);
        Tpl::showpage('member/member_coupon');
    }

    public function member_orderOp(){
        //----------------------------------------
        //--ajax列表基础面板/2016.07.09/黄灿
        //index.php?act=member_index&op=member_order&kind=1
        //----------------------------------------
        $tab_id = $_GET['kind'];

        if(empty($tab_id)){
            $tab_id = "1";
        }

        function set_tab_select($n){
                $a = array();
                for($i=0;$i<=5;$i++){
                    $a[$i]="";
                    if($i==$n){$a[$i]="mui-active";}
                }
                return $a;
        }

    $tab = set_tab_select($tab_id);

        Tpl::output('data',$tab);
        Tpl::showpage('member/member_order');
        
    }

    public function member_order_listOp() {

        //----------------------------------------
        //--ajax响应json输出列表/2016.07.09/黄灿
        //----------------------------------------
        //全部、待付款、待发货、待收货、待评价
        //识别指令
        //识别用户ID
        //检索订单数量
        //检索订单下属商品
        //根据GET指令过滤商品
        //渲染输出商品节点HTML
        
        $kind = $_GET['kind'];
        //var_dump($kind);exit;

        $_SESSION['is_buy'] = true;
       
        $orders = Model('orders');
        $order_goods = Model('order_goods');

        //预备输出容器
        $out_data = array();

        //识别用户ID
        $member_id = $_SESSION['member_id'];
        $order = $orders->where('buyer_id='.$member_id)->select();

        //检索订单下属全部商品
        $order_goods_list = array();

        foreach ($order as $key1) {
            $list = $order_goods->where('order_id='.$key1["order_id"])->select();
            foreach ($list as $key2) {
                array_push($order_goods_list,array_merge($key2,$key1));
            }
        }

        //指令转换
        $commond = $kind;
        $state = "";
        switch ($commond) {
                case '1'://全部
                    $state = "all";
                    break;
                case '2'://待付款
                    $state = "10";
                    break;
                case '3'://待发货
                    $state = "20";
                    break;
                case '4'://待收货
                    $state = "30";
                    break;
                case '5'://待评价
                    $state = "40";
                    break;
                default:
                    $state = "all";
                    break;
        }

        //根据指令过滤商品
        if($state!="all"){
            foreach ($order_goods_list as $key) {
                if($key["order_state"] == $state){
                    if($key['delete_state'] == "0"){
                        array_push($out_data,$key);
                    }
                }
            }
        }else{
            foreach ($order_goods_list as $key) {
                if($key['delete_state'] == "0"){
                    if ($key['order_state'] != "0") {
                            array_push($out_data,$key);
                    }
                }
            }
        }
        
      
        //输出量控制阀
        $num = count($out_data);//查询到的总条数
        $item = array();
        $limit = 3;
        $nid = $_GET["nid"];

        $nid_a = $nid;
        $nid_b = $nid + $limit;
        
        $page = array_slice($out_data,$nid_a,$nid_b);

        if(count($page) != 0){
            foreach ($page as $key) {
                array_push($item,$this->goods($num,$key));
            }
            if(count($page) < $limit){
                array_push($item,"END");
            }
        }else{
            array_push($item,"END");
        }
  
       //输出json
       echo json_encode(array("feedback"=>$item));

       //ajax指令接收
       //
       //取消订单
       //付款
       //退款/退货
       //确认收货
       //评价


    }

    private function goods($index="",$data=""){
    //-------------------------------
    //--AJAX-HTML节点渲染
    //-------------------------------
    global $config;
    $root = $config['upload_site_url'];
    $mobile_root = $config['mobile_site_url'];
    $img = explode("_",$data["goods_image"]);

    $imgpath = $root."/shop/store/goods/".$img[0]."/".$data["goods_image"];
    $add_time = date('Y-m-d H:i:s',$data['add_time']);

    $state = "";
    $btn = "";

switch ($data['order_state']) {

case '0':
$state = "已取消";
$btn = <<<eof

eof;
break;

case '10':
$state = "待付款";
$btn = <<<eof
<!--待付款-->
            <button type='button' class='mui-btn mui-btn-warning mui-btn-outlined'>在线支付</button>
            <button data-get="index.php?act=member_index&op=member_order_cmd&state_type=order_cancel&order_id={$data['order_id']}" type='button' class='mui-btn mui-btn-outlined' data-kind ='2'>取消订单</button>
eof;
break;

case '20':
$state = "待发货";
$btn = <<<eof
<!--待发货-->
            <button type='button' data-kind ='3' class='mui-btn mui-btn-outlined'>退款/退货</button>
eof;
break;

case '30':
$state = "待收货";
$btn = <<<eof
<!--待收货-->
            <a class="mui-btn" style="font-size:14px;padding:6px 12px;border:1px solid #f2f2f2;" href="index.php?act=member_index&op=search_deliver&order_id={$data['order_id']}&order_sn={$data['order_sn']}">查看物流</a><button data-get="index.php?act=member_index&op=member_order_cmd&state_type=order_receive&order_sn={$data['order_sn']}&order_id={$data['order_id']}" type='button' class='mui-btn mui-btn-outlined' data-kind ='4'>确认收货</button>
eof;
break;

case '40':
$state = "交易完成[待评价]";
$btn = <<<eof
<!--待评价-->
            <button type='button' class='mui-btn mui-btn-warning mui-btn-outlined'>评论</button>
            <button data-get="index.php?act=member_index&op=member_order_cmd&state_type=order_delete&order_id={$data['order_id']}" type='button' class='mui-btn mui-btn-outlined' data-kind ='5'>删除订单</button>
eof;
break;

default:
$state = "";
break;
}

return <<<EOF

<div>
    
    <div style="padding: 10px;">
        <label style="line-height: 25px; width: 100%;">
            <img src="{$mobile_root}/resource/images/03.png" style="width: 20px; vertical-align: middle; margin-right: 10px;">
            {$data['store_name']} > <!--{$data['order_state']}-->
            <label>订单编号: {$data["order_sn"]}</label>
            <label style="float: right; color: #BB9E77;">{$state}</label>
            <div style="padding: 10px;"><!--{$index}-->下单时间：{$add_time}</div>
        </label>
    </div>
    <a href="/mobile/index.php?act=goods&op=goods_body&goods_id={$data["goods_id"]}"><div style="background: #F6F6F6; float: left; width: 100%;padding: 10px;">
        <img style="width:100px; float: left;margin-right: 10px;" src="{$imgpath}" />
        <div style="float: left; width: 62%;">
            <h5 style="color: #333333;">{$data["goods_name"]}</h5>

            <div style="line-height: 5px;">
                <span class="color_red">￥{$data["goods_price"]}</span>
                &nbsp;&nbsp;
                <span style="float: right;">X{$data["goods_num"]}</span>
            </div>
        </div>
    </div></a>
    <p class="font_sz_14" style="text-align: right; color: #333333; line-height: 45px;border-bottom: 1px solid #DDDDDD;">共{$data["goods_num"]}件商品 &nbsp; 合计：{$data["goods_pay_price"]} （含运费{$data["shipping_fee"]}）</p>

    <div style=" text-align:right; padding:0 10px 10px 10px;">
        <div style=" text-align:right; padding:0 10px 10px 10px;">

            {$btn}
            

        </div>
    </div>
    <div style="width: 100%; height: 6px; background: #EEEEEE;"></div>
</div>


EOF;


    }



    public function member_order_cmdOp(){
         //-------------------------------
         //订单页面操作指令
         //-------------------------------

        $state_type = $_GET['state_type'];
        $order_id   = intval($_GET['order_id']);
  
        $model_order = Model('order');
        $condition = array();
        $condition['order_id'] = $order_id;
        $condition['buyer_id'] = $_SESSION['member_id'];
        $order_info = $model_order->getOrderInfo($condition);

        //取得其它订单类型的信息
        $model_order->getOrderExtendInfo($order_info);

        $result = null;



        switch ($state_type) {
            case 'order_cancel':
                $result = $this->order_cancel($order_info);//取消订单
                break;
            case 'order_receive':

                $result = $this->order_receive($order_info);//确认收货

                break;
            case 'order_delete':
                $result = $this->order_recycle($order_info);//订单入回收站
                break;
            default:
                exit();
                break;
        }

        echo json_encode($result);
        //
        //var_dump($result);

    }

    /**
     * 取消订单
     */
    private function order_cancel($order_info) {

            $result = null;

            $model_order = Model('order');
            $logic_order = Logic('order');
            $if_allow = $model_order->getOrderOperateState('buyer_cancel',$order_info);
            if (!$if_allow) {
                $result = array('state' =>false,'msg' =>'无权操作','data' =>"");
            }
            if (TIMESTAMP - 86400 < $order_info['api_pay_time']) {
                $_hour = ceil(($order_info['api_pay_time']+86400-TIMESTAMP)/3600);
                $result = array('state' =>false,'msg' =>'该订单曾尝试使用第三方支付平台支付，须在'.$_hour.'小时以后才可取消','data' =>"");
            }

            $msg = "未设置取消原因。";
            if ($order_info['order_type'] != 2) {
                $cancel_condition = array();
                if ($order_info['payment_code'] != 'offline') {
                    $cancel_condition['order_state'] = ORDER_STATE_NEW;
                }
                $result = $logic_order->changeOrderStateCancel($order_info,'buyer', $_SESSION['member_name'], $msg,true,$cancel_condition);
            } else {
                //取消预定订单
                $result = Logic('order_book')->changeOrderStateCancel($order_info,'buyer', $_SESSION['member_name'], $msg);
            }

        return $result;
    }

    /**
     * 收货
     */
    private function order_receive($order_info) {

        $result = NULL;

        $model_order = Model('order');
        $logic_order = Logic('order');
        $if_allow = $model_order->getOrderOperateState('receive',$order_info);
        if (!$if_allow) {
                $result = array('state' =>false,'msg' =>'无权操作','data' =>"");
        }

        $result = $logic_order->changeOrderStateReceive($order_info,'buyer',$_SESSION['member_name'],'签收了货物');

        return $result;
    }

    /**
     * 放入回收站
     */
    private function order_recycle($order_info){

        $result = NULL;

        $model_order = Model('order');
        $logic_order = Logic('order');
        $state_type = str_replace(array('order_delete','order_drop','order_restore'), array('delete','drop','restore'), $_GET['state_type']);
        $if_allow = $model_order->getOrderOperateState($state_type,$order_info);
        if (!$if_allow) {
            $result = array('state' =>false,'msg' =>'无权操作','data' =>"");
        }

        $result =  $logic_order->changeOrderStateRecycle($order_info,'buyer',$state_type);

        return $result;
    }

     public function money_recordOp(){
         $model_member = Model('member');
         $member_info = $model_member->getMemberInfoByID($_SESSION['member_id']);
         Tpl::output('member_info',$member_info);
         Tpl::showpage('member/money_record');
    }

    public function integralOp(){
        $member_info = array();
        if($_SESSION['member_id']) {
            $model_member = Model('member');
            $member_info = $model_member->getMemberInfoByID($_SESSION['member_id']);
            if ($member_info){
                $member_gradeinfo = $model_member->getOneMemberGrade(intval($member_info['member_exppoints']));
                $member_info = array_merge($member_info,$member_gradeinfo);
                $member_info['security_level'] = $model_member->getMemberSecurityLevel($member_info);
            }
        }

        $member_info['user_name'] = $this->member_info['member_name'];
        $member_info['avatar'] = getMemberAvatarForID($this->member_info['member_id']);
        $member_info['point'] = $this->member_info['member_points'];
        $member_info['predepoit'] = $this->member_info['available_predeposit'];
        $member_info['available_rc_balance'] = $this->member_info['available_rc_balance'];
        $model_order = Model('order');
        $member_info['order_nopay_count'] = $model_order->getOrderCountByID('buyer',$_SESSION['member_id'],'NewCount');
        $member_info['order_noreceipt_count'] = $model_order->getOrderCountByID('buyer',$_SESSION['member_id'],'SendCount');
        $member_info['order_noeval_count'] = $model_order->getOrderCountByID('buyer',$_SESSION['member_id'],'EvalCount');

        $member_info['favorites_goods'] = Model('favorites')->getGoodsFavoritesCountByGoodsId(array(),$_SESSION['member_id']);
        $member_info['favorites_store'] = Model('favorites')->getStoreFavoritesCountByStoreId(array(),$_SESSION['member_id']);
        $member_info['voucher_count'] = Model('voucher')->getCurrentAvailableVoucherCount($_SESSION['member_id']);
        $member_info['redpacket_count'] = Model('redpacket')->getCurrentAvailableRedpacketCount($_SESSION['member_id']);
        Tpl::output('home_member_info',$member_info);
        $condition_arr = array();
        $condition_arr['pl_memberid'] = $_SESSION['member_id'];
        if ($_GET['stage']){
            $condition_arr['pl_stage'] = $_GET['stage'];
        }
        $condition_arr['saddtime'] = strtotime($_GET['stime']);
        $condition_arr['eaddtime'] = strtotime($_GET['etime']);
        if($condition_arr['eaddtime'] > 0) {
            $condition_arr['eaddtime'] += 86400;
        }
        $condition_arr['pl_desc_like'] = $_GET['description'];

        
        //兑换信息列表
//        $where = array();
//        $where['point_buyerid'] = $_SESSION['member_id'];
//
//        $model_pointorder = Model('pointorder');
//        $order_list = $model_pointorder->getPointOrderList($where, '*', 10, 0, 'point_orderid desc');
//        $order_idarr = array();
//        $order_listnew = array();
//        if (is_array($order_list) && count($order_list)>0){
//            foreach ($order_list as $k => $v){
//                $order_listnew[$v['point_orderid']] = $v;
//                $order_idarr[] = $v['point_orderid'];
//            }
//        }
//
//        //查询兑换商品
//        if (is_array($order_idarr) && count($order_idarr)>0){
//            $prod_list = $model_pointorder->getPointOrderGoodsList(array('point_orderid'=>array('in',$order_idarr)));
//            if (is_array($prod_list) && count($prod_list)>0){
//                foreach ($prod_list as $v){
//                    if (isset($order_listnew[$v['point_orderid']])){
//                        $order_listnew[$v['point_orderid']]['prodlist'][] = $v;
//                    }
//                }
//            }
//        }
//
//        //信息输出
//        Tpl::output('order_list',$order_listnew);
//        Tpl::output('page',$model_pointorder->showpage(2));
//        self::profile_menu('pointorder','orderlist');
//


        //分页
        $page   = new Page();
        $page->setEachNum(5);
        $page->setStyle('admin');
        //查询积分日志列表
        $points_model = Model('points');
        $list_log = $points_model->getPointsLogList($condition_arr,$page,'*','');
        //信息输出
        Tpl::output('show_page',$page->show());
        Tpl::output('list_log',$list_log);
        Tpl::showpage('member/member_integral');


    }

    //性别修改
    public function sex_modifyOp(){
        $member_id=$_SESSION['member_id'];
        $model_modify = Model('member');
        $member_sex= $_POST['member_sex'];

        $model_show = Model('member');
        $member_name = $model_modify->memberShow($member_id);


        $member_modify_sex = $model_modify->sex_modify($member_id, $member_sex);


        if($member_modify_sex){
            echo "<script>alert('修改成功');window.location.href=\"/mobile/index.php?act=member_index&op=member_setting\";  
         </script>";

        }else{
            echo "<script>alert('修改失败，请重试！')</script>";
            echo "<script>history.go(-1);</script>";
        }
       
    }
    public function member_rechargeOp(){
        Tpl::showpage('member/member_recharge');
    }

    public function recharge_addOp(){
        if (!chksubmit()){
            echo "<script>alert('请输入充值金额');window.location.href='/mobile/index.php?act=member_index&op=member_recharge';</script>";
        }
        $pdr_amount = abs(floatval($_POST['pdr_amount']));
        if ($pdr_amount <= 0) {
            showMessage(Language::get('predeposit_recharge_add_pricemin_error'),'','html','error');
        }
        $model_pdr = Model('predeposit');
        $data = array();
        $data['pdr_sn'] = $pay_sn = $model_pdr->makeSn();
        $data['pdr_member_id'] = $_SESSION['member_id'];
        $data['pdr_member_name'] = $_SESSION['member_name'];
        $data['pdr_amount'] = $pdr_amount;
        $data['pdr_add_time'] = TIMESTAMP;
        $insert = $model_pdr->addPdRecharge($data);
//        session_start();

        if ($insert) {
            //转向到商城支付页面
            redirect(MOBILE_SITE_URL . '/api/payment/alipay/alipayapi.php?pay_sn='.$pay_sn.'&amount='.$pdr_amount);
        }
    }
    /*
     * 物流信息
     *
     * **/
    public function search_deliverOp(){
        Language::read('member_member_index');
        $lang   = Language::getLangContent();
        $order_id   = intval($_GET['order_id']);
        if ($order_id <= 0) {
            showMessage(Language::get('wrong_argument'),'','html','error');
        }

        $model_order    = Model('order');
        $condition['order_id'] = $order_id;
        $condition['buyer_id'] = $_SESSION['member_id'];
        $order_info = $model_order->getOrderInfo($condition,array('order_common','order_goods'));
        if (empty($order_info) || !in_array($order_info['order_state'],array(ORDER_STATE_SEND,ORDER_STATE_SUCCESS))) {
            showMessage('未找到信息','','html','error');
        }
        Tpl::output('order_info',$order_info);

        //卖家信息
        $model_store    = Model('store');
        $store_info     = $model_store->getStoreInfoByID($order_info['store_id']);
        Tpl::output('store_info',$store_info);

        //卖家发货信息
        $daddress_info = Model('daddress')->getAddressInfo(array('address_id'=>$order_info['extend_order_common']['daddress_id']));
        Tpl::output('daddress_info',$daddress_info);

        //取得配送公司代码
        $express = rkcache('express',true);
        Tpl::output('e_code',$express[$order_info['extend_order_common']['shipping_express_id']]['e_code']);
        Tpl::output('e_name',$express[$order_info['extend_order_common']['shipping_express_id']]['e_name']);
        Tpl::output('e_url',$express[$order_info['extend_order_common']['shipping_express_id']]['e_url']);
        Tpl::output('shipping_code',$order_info['shipping_code']);

        self::profile_menu('search','search');
        Tpl::output('left_show','order_view');
        Tpl::showpage('member/member_order_deliver');
    }
    /**
     * 从第三方取快递信息
     *
     */
    public function get_expressOp(){

        $content = Model('express')->get_express($_GET['e_code'], $_GET['shipping_code']);

        $output = array();
        foreach ($content as $k=>$v) {
            if ($v['time'] == '') continue;
            $output[]= $v['time'].'&nbsp;&nbsp;'.$v['context'];
        }
        if (empty($output)) exit(json_encode(false));
        echo json_encode($output);
    }


}

